package com.jeeplus.test.jlnu.teachingArchive.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.test.jlnu.teachingArchive.domain.entity.InsideInfo;
import com.jeeplus.test.jlnu.teachingArchive.domain.entity.RecordsInfo;
import com.jeeplus.test.jlnu.teachingArchive.domain.vo.RecordsInfoVo;
import com.jeeplus.test.jlnu.teachingArchive.mapper.RecordsInfoMapper;
import com.jeeplus.test.jlnu.teachingArchive.service.RecordsInfoService;
import com.jeeplus.test.tools.resp.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class RecordsInfoServiceImpl extends ServiceImpl<RecordsInfoMapper, RecordsInfo> implements RecordsInfoService {
    @Autowired
    private RecordsInfoMapper recordsInfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveAndLinkInside(RecordsInfoVo recordsInfoVo) {
        if (recordsInfoVo.getIds() == null || recordsInfoVo.getIds().trim().isEmpty()) {
            return R.error("9999", "ids参数不能为空");
        }

        List<String> idList = Arrays.asList(recordsInfoVo.getIds().split(","));
        if (CollectionUtils.isEmpty(idList)) {
            return R.error("9999", "ids解析后为空");
        }

        RecordsInfo recordsInfo = new RecordsInfo();
        BeanUtils.copyProperties(recordsInfoVo, recordsInfo);

        boolean saveSuccess = save(recordsInfo);
        if (!saveSuccess) {
            return R.error("9999", "新增档案信息失败");
        }

        String recordsId = recordsInfo.getId();

        int updateCount = baseMapper.batchUpdateInsideRecordsId(recordsId, idList);
        if (updateCount <= 0) {
            return R.suc("档案信息新增成功，但未更新到另一张表数据");
        }

        return R.suc("操作成功");
    }
    @Override
    public RecordsInfoVo getDetailById(String id) {
        RecordsInfoVo detailVo = new RecordsInfoVo();
        RecordsInfo recordsInfo = recordsInfoMapper.selectById(id);
        if (recordsInfo == null) {
            return detailVo;
        }
        detailVo.setRecordsInfo(recordsInfo);

        List<InsideInfo> insideList = recordsInfoMapper.selectInsideListByRecordsId(id);
        detailVo.setInsideInfoList(insideList);

        return detailVo;
    }
    @Override
    public RecordsInfoVo saveOrUpdate(RecordsInfoVo vo){
        RecordsInfo recordsInfo = new RecordsInfo();
        BeanUtils.copyProperties(vo,recordsInfo);

        boolean isSuccess = super.saveOrUpdate(recordsInfo);
        if (!isSuccess) {
            return null;
        }
        RecordsInfoVo resultVo = new RecordsInfoVo();
        resultVo.setRecordsInfo(recordsInfo);
        return resultVo;
    }
    @Override
    public IPage<RecordsInfoVo> page(Page<RecordsInfo> page,RecordsInfo query){
        QueryWrapper<RecordsInfo> wrapper = new QueryWrapper<>(query)
                .orderByDesc("form_date");
        IPage<RecordsInfo> entityPage =baseMapper.selectPage(page,wrapper);
        IPage<RecordsInfoVo> voPage = new Page<>(
                entityPage.getCurrent(),
                entityPage.getSize(),
                entityPage.getTotal()
        );
        List<RecordsInfoVo> voList = entityPage.getRecords().stream().map(recordsInfo -> {
            RecordsInfoVo vo = new RecordsInfoVo();
            vo.setRecordsInfo(recordsInfo);
            return vo;
        }).collect(Collectors.toList());
        voPage.setRecords(voList);
        return voPage;
    }
}
